<template>
	<div class="system-menu-container layout-pd">
		<el-card shadow="hover">
			<div class="system-menu-search mb15">
				<el-button size="default" type="primary" class="ml10" @click="clickExpand">
					<el-icon><ele-Sort /></el-icon>
					{{ expand.state ? "全部关闭" : "全部展开" }}
				</el-button>

        <el-button size="default" type="success" class="ml10" @click="onOpenAddMenu('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增菜单
				</el-button>

			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%" row-key="id"
			:key="expand.key" :default-expand-all="expand.state"  :expand-row-keys="expandArr"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
				<el-table-column label="菜单名称" width="250" show-overflow-tooltip>
					<template #default="scope">
						<SvgIcon :name="scope.row.icon" />
						<span class="ml10">{{ $t(scope.row.name) }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="path" label="路由路径" width="180" show-overflow-tooltip></el-table-column>
				<el-table-column  label="组件路径" show-overflow-tooltip>
					<template #default="scope">
						<span v-if="scope.row.type == 1&&scope.row.component.length>0" > <el-tag type="warning" size="small">/@/views/</el-tag><el-tag size="small">{{
							scope.row.component }}</el-tag> <el-tag type="warning" size="small">.vue</el-tag></span>
					</template>
				</el-table-column>
				<el-table-column label="权限标识" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ scope.row.permission }}</span>
					</template>
				</el-table-column>
				<el-table-column label="排序" show-overflow-tooltip width="80">
					<template #default="scope">
						{{ scope.row.sort }}
					</template>
				</el-table-column>
				<el-table-column label="类型" show-overflow-tooltip width="90">
					<template #default="scope">
						<el-tag :type="scope.row.type == 1? 'success':'danger'" size="small">{{ scope.row.type == 1 ? '菜单' : '按钮' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditMenu('add', scope.row)">新增</el-button>
						<el-button size="small" text type="primary"
							@click="onOpenEditMenu('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<MenuDialog ref="menuDialogRef" @refresh="formSubmit" />
	</div>
</template>

<script setup lang="ts" name="systemMenu">
import { defineAsyncComponent, ref, onMounted, reactive } from 'vue';
import { RouteRecordRaw } from 'vue-router';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useMenuApi } from '/@/api/system/menu/index';
import { isEmpty, tansParams } from '/@/utils/mms';

// import { setBackEndControlRefreshRoutes } from "/@/router/backEnd";

// 引入组件
const MenuDialog = defineAsyncComponent(() => import('/@/views/system/menu/dialog.vue'));
// 引入 api 请求接口
const baseApi = useMenuApi();
// 定义变量内容
// 展开/合闭
const expand = ref({
	state: false,
	key: 0
});
// 默认展开行
let expandArr = ref<string[]>([]);
const menuDialogRef = ref();
const state = reactive({
	tableData: {
		data: [] as RouteRecordRaw[],
		loading: true,
	},
});

// 接受子组件传值
const formSubmit = (row: FunctionType) => {

	if (isEmpty(row.id)) {
		//新增
		baseApi.insert(row).then(row => {
			menuDialogRef.value.closeDialog();
			ElMessage.success(row.msg)
			setTimeout(() => {
				getTableData();
			}, 1000)
		}).catch(async err => { ElMessage.warning(err); }).finally(() => { })
	} else {
		//更新
		baseApi.edit(row).then(row => {
			menuDialogRef.value.closeDialog();
			ElMessage.success(row.msg)
			setTimeout(() => {
				getTableData();
			}, 1000)
		}).catch(async err => { ElMessage.warning(err); }).finally(() => { })
	}

}

// 展开或折叠全部的方法
const clickExpand = () => {
	expand.value.key = +new Date();
	expand.value.state = !expand.value.state;
}

// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	baseApi.list().then(res => {
		state.tableData.data = res.data;
		state.tableData.loading = false;
		expandArr.value.push(String(1));
	})
};
// 打开新增菜单弹窗
const onOpenAddMenu = (type: string) => {
  console.log(type)
  baseApi.query(1).then(res => {
    menuDialogRef.value.openDialog(type, res.data);
  }).catch(async err => { ElMessage.warning(err); }).finally(() => { })
};
// 打开编辑菜单弹窗
const onOpenEditMenu = (type: string, row: FunctionType) => {
  console.log(type)
	baseApi.query(row.id).then(res => {
		menuDialogRef.value.openDialog(type, res.data);
	}).catch(async err => { ElMessage.warning(err); }).finally(() => { })
};
// 删除当前行
const onTabelRowDel = (row: FunctionType) => {
	ElMessageBox.confirm(`此操作将永久删除路由：${row.path}, 是否继续?`, '提示', {
		confirmButtonText: '删除',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
      baseApi.delete(row.id).then(res => {
        getTableData();
        ElMessage.success('删除成功');
      }).catch(async err => { ElMessage.warning(err); }).finally(() => { })
			//await setBackEndControlRefreshRoutes() // 刷新菜单，未进行后端接口测试
		})
		.catch(() => { });
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
<style>
el-table .el-icon{
  color: #000;
}
.el-tooltip i{
  color: #000;
}
</style>
